package com.it.controller;

import com.it.VO.CommentVo;
import com.it.common.Result;
import com.it.domain.Comment;
import com.it.domain.MessageComment;
import com.it.domain.User;
import com.it.service.CommentService;
import com.it.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping("/Comment")
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private LoginService loginService;

    @PostMapping("/SaveComment")
    public Result SaveComment(@RequestBody Comment comment, HttpServletRequest request){
        String token = request.getHeader("token");
        if(token == null){
            return Result.fail("404","请登录");
        }
        User user = loginService.CheckToken(token);
        if(user == null){
            return Result.fail("404","请登录");
        }
        if(comment.getToCommUserId() == null){
            comment.setToCommUserId(-1);
        }
        if(comment.getParentId() == null){
            comment.setParentId(-1);
        }
        if(comment.getRootId() == null){
            comment.setRootId(-1);
        }
        String Content = comment.getContent().trim();
        if(Content == null || Content == ""){
            return Result.fail("404","内容为空");
        }
        comment.setUserId(user.getId());
        return commentService.SaveComment(comment);
    }

    @PostMapping("/getAllComment")
    public List<CommentVo> getAllComment(@RequestParam Integer commType,@RequestParam Integer commTargetId){
        System.out.println("hellopdkdod");
        List<CommentVo> commentVoList = commentService.getMainComment(commType,commTargetId);
        if(commentVoList == null){
            return null;
        }
        Map<Integer, CommentVo> map = new HashMap<>();
        Map<Integer,CommentVo> result = new HashMap<>();
        List<CommentVo> data = new ArrayList<>();
        for(CommentVo commentVo : commentVoList){
            if(commentVo.getRootId() == -1){
                result.put(commentVo.getId(),commentVo);
                System.out.println(result.get(commentVo.getId()).toString());
            }
            map.put(commentVo.getRootId(),commentVo);
        }

        for(CommentVo commentVo : commentVoList){
            Integer id = commentVo.getRootId();
            if(id != -1){
                if(commentService.SelectRootId(id,commentVo.getCommTargetId(),commentVo.getCommType()).size()==0)
                    continue;
                else {
                    if (result.get(id).getChildren() == null) {
                        result.get(id).setChildren(new ArrayList<>());
                    }
                    result.get(id).getChildren().add(commentVo);
                }
            }
        }
        for(CommentVo commentVo : commentVoList){
            if(commentVo.getRootId() == -1){
                data.add(result.get(commentVo.getId()));
            }
        }
        return data;
    }

    @PostMapping("/DeleteComment")
    public Result DeleteComment(@RequestBody Comment comment){

        Queue<Comment> queue = new LinkedList<>();
        if(comment.getRootId() == -1){
            commentService.DeleteComment(comment);
            commentService.DeleteRootComment(comment);
        }
        else{
            queue.offer(comment);
            while(queue.peek() != null){
                Comment comment1 = queue.peek();
                List<Comment> CommentList = commentService.SelectByParentId(comment1);
               commentService.DeleteComment(comment1);
                queue.poll();
                for(Comment comment2 : CommentList){
                    if(comment2 != null){
                        queue.offer(comment2);
                    }
                }
            }
        }

        return Result.success("200","删除成功");
    }
}
